
package GUIs;

import Configs.Config;
import Controllers.NguoiThueController;
import models.GioiTinh;
import models.NguoiThue;


public class f_nhanvien_nguoithue_sua extends javax.swing.JFrame {
    
    private static f_nhanvien_nguoithue_sua _instance; //Kiểm tra sự tồn tại của 2 instance trong cùng 1 form
    private final Config conn;
    private NguoiThueController nguoithuecontrol;
    private NguoiThue nguoithuesua;
  
    public f_nhanvien_nguoithue_sua(Config conn) {
        this.conn = conn;
        initComponents();
    }
        
    public static f_nhanvien_nguoithue_sua getInstance(Config conn, String cmnd) {
        if (_instance == null) {
            _instance = new f_nhanvien_nguoithue_sua(conn);
        }
        // Lam moi lai noi dung
        _instance.nguoithuecontrol = new NguoiThueController(_instance.conn);
        _instance.nguoithuesua = _instance.nguoithuecontrol.getWithCMND(cmnd);
        _instance.resetForm();
        return _instance;
    }

    public void createAndShowUI() {
        setVisible(true);
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnthemnv = new javax.swing.JPanel();
        lbcmnd = new javax.swing.JLabel();
        tfcmnd = new javax.swing.JTextField();
        tfhoten = new javax.swing.JTextField();
        lbhoten = new javax.swing.JLabel();
        lbgioitinh = new javax.swing.JLabel();
        lbngaysinh = new javax.swing.JLabel();
        tfsdt = new javax.swing.JTextField();
        lbsdt = new javax.swing.JLabel();
        lbcongviec = new javax.swing.JLabel();
        cbbgioitinh = new javax.swing.JComboBox();
        dpngaysinh = new org.jdesktop.swingx.JXDatePicker();
        btnluu = new javax.swing.JButton();
        btnreset = new javax.swing.JButton();
        btnthoat = new javax.swing.JButton();
        spcongviec = new javax.swing.JScrollPane();
        textareacongviec = new javax.swing.JTextArea();
        lbtieude = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Sửa thông tin người thuê");
        setResizable(false);
        setType(java.awt.Window.Type.UTILITY);

        lbcmnd.setText("CMND:");

        tfcmnd.setEditable(false);

        lbhoten.setText("Họ tên:");

        lbgioitinh.setText("Giới tính:");

        lbngaysinh.setText("Ngày sinh:");

        lbsdt.setText("Số điện thoại:");

        lbcongviec.setText("Công việc:");

        cbbgioitinh.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Chọn", "Nam", "Nữ" }));

        btnluu.setText("Lưu");
        btnluu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnluuActionPerformed(evt);
            }
        });

        btnreset.setText("Làm lại");
        btnreset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnresetActionPerformed(evt);
            }
        });

        btnthoat.setText("Thoát");
        btnthoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnthoatActionPerformed(evt);
            }
        });

        textareacongviec.setColumns(20);
        textareacongviec.setRows(5);
        spcongviec.setViewportView(textareacongviec);

        lbtieude.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        lbtieude.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbtieude.setText("Sửa thông tin người thuê");

        javax.swing.GroupLayout pnthemnvLayout = new javax.swing.GroupLayout(pnthemnv);
        pnthemnv.setLayout(pnthemnvLayout);
        pnthemnvLayout.setHorizontalGroup(
            pnthemnvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lbtieude, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(pnthemnvLayout.createSequentialGroup()
                .addGroup(pnthemnvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnthemnvLayout.createSequentialGroup()
                        .addGap(60, 60, 60)
                        .addGroup(pnthemnvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(pnthemnvLayout.createSequentialGroup()
                                .addComponent(lbgioitinh)
                                .addGap(64, 64, 64)
                                .addComponent(cbbgioitinh, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(pnthemnvLayout.createSequentialGroup()
                                .addComponent(lbngaysinh)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(dpngaysinh, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(pnthemnvLayout.createSequentialGroup()
                                .addComponent(lbsdt)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(tfsdt, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, pnthemnvLayout.createSequentialGroup()
                                .addComponent(lbcongviec)
                                .addGap(55, 55, 55)
                                .addComponent(spcongviec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(pnthemnvLayout.createSequentialGroup()
                                .addGroup(pnthemnvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lbhoten)
                                    .addComponent(lbcmnd))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(pnthemnvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(tfhoten, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                                    .addComponent(tfcmnd)))))
                    .addGroup(pnthemnvLayout.createSequentialGroup()
                        .addGap(93, 93, 93)
                        .addComponent(btnluu, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnreset)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnthoat)))
                .addContainerGap(64, Short.MAX_VALUE))
        );
        pnthemnvLayout.setVerticalGroup(
            pnthemnvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnthemnvLayout.createSequentialGroup()
                .addComponent(lbtieude, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(23, 23, 23)
                .addGroup(pnthemnvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfcmnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbcmnd, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnthemnvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbhoten, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfhoten, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnthemnvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbgioitinh)
                    .addComponent(cbbgioitinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnthemnvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lbngaysinh, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dpngaysinh, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnthemnvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbsdt, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfsdt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(pnthemnvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnthemnvLayout.createSequentialGroup()
                        .addGap(42, 42, 42)
                        .addComponent(lbcongviec, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnthemnvLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(spcongviec, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(35, 35, 35)
                .addGroup(pnthemnvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnluu)
                    .addComponent(btnreset)
                    .addComponent(btnthoat))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(pnthemnv, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnthemnv, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btnluuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnluuActionPerformed
        if (this.nguoithuecontrol.Edit(this)) {
            f_nhanvien change = f_nhanvien.getInstance(this.conn, null);
            change.refreshNguoiThue();
            this.dispose();
        }
    }//GEN-LAST:event_btnluuActionPerformed

    private void btnresetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnresetActionPerformed
       this.resetForm();
    }//GEN-LAST:event_btnresetActionPerformed

    private void btnthoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnthoatActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnthoatActionPerformed

    private void resetForm()
    {
        this.tfcmnd.setText(this.nguoithuesua.getCMND());
        this.tfhoten.requestFocus();
        this.tfhoten.setText(this.nguoithuesua.getHoten());
        this.cbbgioitinh.setSelectedIndex((this.nguoithuesua.getGioitinh() == GioiTinh.Nam)? 1 : 2);
        this.dpngaysinh.setDate(this.nguoithuesua.getNgaysinh());
        this.tfsdt.setText(this.nguoithuesua.getSdt());
        this.textareacongviec.setText(this.nguoithuesua.getCongviec());
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnluu;
    private javax.swing.JButton btnreset;
    private javax.swing.JButton btnthoat;
    public javax.swing.JComboBox cbbgioitinh;
    public org.jdesktop.swingx.JXDatePicker dpngaysinh;
    private javax.swing.JLabel lbcmnd;
    private javax.swing.JLabel lbcongviec;
    private javax.swing.JLabel lbgioitinh;
    private javax.swing.JLabel lbhoten;
    private javax.swing.JLabel lbngaysinh;
    private javax.swing.JLabel lbsdt;
    private javax.swing.JLabel lbtieude;
    private javax.swing.JPanel pnthemnv;
    private javax.swing.JScrollPane spcongviec;
    public javax.swing.JTextArea textareacongviec;
    public javax.swing.JTextField tfcmnd;
    public javax.swing.JTextField tfhoten;
    public javax.swing.JTextField tfsdt;
    // End of variables declaration//GEN-END:variables
}
